const app = getApp();
var bridgeConfig = require('../../../../utils/bridge.js')
var convert2ChartData = require('../../../../utils/dataConvert.js')

Page({
  data: {
    id: null,
    userId: '',
    userName: '',
    editable: false,
    bridgeData: null,
    bridgeInfo: bridgeConfig.bridgeInfo,
    bridgeIndex: bridgeConfig.bridgeIndex,
    bridgeEfficiencyIndex: bridgeConfig.bridgeEfficiencyIndex,
    extfeild: '',
    flag: true,
    myBrdAttrMap: null,
    othBrdAttrMap: null,
  },

  onLoad: function (option) {
    var structureId = '';
    var token = app.globalData.openId;

    if (option && option.bridgeInfo) {
      let currentBridgeInfo = JSON.parse(option.bridgeInfo);
      this.setData({
        bridgeData: currentBridgeInfo,
        id: currentBridgeInfo.id,
        userId: currentBridgeInfo.userId,
        userName: currentBridgeInfo.userName
      });

      this.initBridgeData(currentBridgeInfo);
    }
    this.getOther();
  },

  // 拿到其他桥梁
  getOther: function () {
    var that = this;
    let otherBridges = wx.getStorageSync('otherBridges');
    that.setData({
      otherBridges: otherBridges,
      othBrdAttrMap: convert2ChartData.convert2ChartData(otherBridges)
    });
  },

  viewBridgeIndex: function (e) {
    let key = e.currentTarget.dataset.itemname + '-' + this.data.bridgeData.structureTypeName;
    let indexInfo = {
      structureName: this.data.bridgeData.structureTypeName,  // 桥梁结构类型
      spanExpression: this.data.bridgeData.spanExpression,    // 跨径表达式
      averageSpan: this.data.bridgeData.avgAcross,            // 平均跨径
      bridgeWidth: this.data.bridgeData.width,                // 桥宽
      title: e.currentTarget.dataset.title,                   // 指标图页面标题
      description: e.currentTarget.dataset.description,       // 指标图页面标题描述
      itemTitle: e.currentTarget.dataset.itemtitle,           // Y轴标题（含单位）
      itemName: e.currentTarget.dataset.itemname,             // 指标名字
      itemUnit: e.currentTarget.dataset.unit,                 // 该指标Y坐标单位
      itemValue: e.currentTarget.dataset.itemvalue,          // 该指标Y坐标值
      myBridgeIndexData: this.data.myBrdAttrMap ? this.data.myBrdAttrMap.get(key) : [],
      otherBridgeIndexData: this.data.othBrdAttrMap ? this.data.othBrdAttrMap.get(key) : []
    };

    wx.navigateTo({
      url: '/pages/show/show_chart/show_chart?indexInfo=' + JSON.stringify(indexInfo)
    });
  },
  
  //审批备注
  extInput:function(e){
    var that = this;
    var text = e.detail.value;
    that.setData({
      extfeild:e.detail.value
    })
  },

  initBridgeData: function (bridgeData) {
    for (let item of this.data.bridgeInfo) {
      item["val"] = bridgeData[item.name];
      item["indexType"] = 'bridgeInfo';
      item["displayTitle"] = item.unit ? item.title + '(' + item.unit + ')' : item.title;
    }

    this.setData({
      bridgeInfo: this.data.bridgeInfo
    });

    for (let item of this.data.bridgeIndex) {
      item["val"] = bridgeData[item.name];
      item["indexType"] = 'bridgeIndex';
      item["displayTitle"] = item.unit ? item.title + '(' + item.unit + ')' : item.title;
    }

    this.setData({
      bridgeIndex: this.data.bridgeIndex
    });

    for (let item of this.data.bridgeEfficiencyIndex) {
      item["val"] = bridgeData[item.name];
      item["indexType"] = 'bridgeEfficiencyIndex';
      item["displayTitle"] = item.unit ? item.title + '(' + item.unit + ')' : item.title;
    }

    this.setData({
      bridgeEfficiencyIndex: this.data.bridgeEfficiencyIndex
    });
  },
 
  reject:function(){
    this.audit(2, this.data.extfeild)
  },

  agree:function(){
    this.audit(1,this.data.extfeild);
  },

  audit: function (status, extfeild){
    var id = this.data.id;
    wx.request({
      url: app.globalData.siteUrl + '/bridge/submit',
      method: 'post',
      header: {
        "X-Token": wx.getStorageSync("openId"),
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: {
        id: id,
        status: status,
        extendFields: extfeild
      },
      success: function (res) {
        if (res.data == null || !res.data.success) {
          wx.showToast({
            title: res.data.msg == null || res.data == undefined ? '审核失败' : "审核失败:" + res.data.msg,
            icon: 'success',
            duration: 3000
          });
        }
        wx.showToast({
          title: "审核完成",
          icon: 'success',
          duration: 3000
        });

        wx.navigateBack();
      },
      error: function (res) {
        wx.showToast({
          title: '审核失败',
          icon: 'success',
          duration: 3000
        });
        wx.navigateBack();
      }
    });
  },

})